<?php
class UsersController extends AppController {

	var $name = 'Users';
    var $layout = false;
    var $helper = array("Html");
    var $component = array('Auth','Session');
    var $_sessionUsername = "Username";
    
    function view2(){
        if(!$this->Session->read($this->_sessionUsername)){
            $this->redirect("login");
        }else{
            //$this->render("trangadmin");
			$this->redirect("/");
        }
    }

    function login(){
        $error="a";
        if($this->Session->read($this->_sessionUsername)){
            $this->redirect("view2");
        }
        if(isset($_POST['ok'])){
            $username=$_POST['username'];
            $password = $_POST['password'];
			
			if(empty($username)){
				$error="Username khong duoc de trong";
			}
			if(empty($password)){
				$error="Password khong duoc de trong";
			}
			if(empty($username)&&empty($password)){
				$error="Username va password khong duoc de trong";
			}
			if(!empty($username) && !empty($password)) {
				$hashpw=sha1($password);
				if($this->User->checkLogin($username,$hashpw)){
				   $this->Session->write($this->_sessionUsername,$username);
				   $this->redirect("view2"); 
				}else{
					$error="Username or Password nhap khong dung";					
				}
			}
			$this->set("error",$error);
			$this->render("login");
        }
        
    }
	function trangadmin(){
	}
	    /*
	function login1(){
		if(isset($_POST['submit'])) {
		$errors = array();
		$required = array('username','pword');
		foreach($required as $fieldname) {
			if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
				$errors[]= "The <strong> {$fieldname} </strong> was left blank";
			}
		}//End: foreach
		if(empty($errors)) {
			
			$username = $_POST['username'];
			$pword = $_POST['pword'];
			$hash_pw = sha1($pword);
			if($this->User->checkLogin($username,$hash_pw)){
               $this->Session->write($this->_sessionUsername,$username);
               $this->redirect("view2");
            }else{
                $errors[] = "The username or password do not match those on file.";
            }
			
		}
		
		}//End if($_POST['submit'])
		$this->set("errors",$errors);
		$this->render("login1");
	}
	*/
	function logout2(){
			if(isset($_GET['stat']) && $_GET['stat'] == 1) {
				$message = "<ul><li>You are now logged out.</li></ul>";
				return $message;
			}
        $this->Session->delete($this->_sessionUsername);
        $this->redirect("/");
    }
	
	//-------------------------------------------tao moi-------------------------------
	function logindemo(){
	//$this->layout='trangchu';
		if(isset($_POST['submit'])) {
		$errors = array();
		$required = array('username','pword');
		foreach($required as $fieldname) {
			if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
				$errors[]= "The <strong> {$fieldname} </strong> was left blank";
				$this->set("errors",$errors);
				$this->redirect("../posts/tinvip?err='The <strong> {$fieldname} </strong> was left blank'");
			}
		}//End: foreach
		if(empty($errors)) {
			
			$username = $_POST['username'];
			$pword = $_POST['pword'];
			$hash_pw = sha1($pword);
			if($this->User->checkLogin($username,$hash_pw)){
               $this->Session->write($this->_sessionUsername,$username);
               $this->redirect("view2");
            }else{
                $errors[] = "The username or password do not match those on file.";
				$this->render("login");
            }
			
		}
			$this->set("errors",$errors);
			
		}//End if($_POST['submit'])
		
		}
		
    function logout(){
        $this->Session->delete($this->_sessionUsername);
        $this->redirect("login");
    }
    
	function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
		//$this-> layout = 'user_layout';
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		$this->layout = 'user_hoan';
		$ten = $this->Session->read("Username");
		$datas = $this->User->find('all',array('conditions' => array('User.user_name' => $ten)));
		$user = $datas[0];
		$id=$user['User']['id'];
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'thongtincanhan'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	function thongtincanhan(){
		$this->layout = 'user_hoan';
		$ten = $this->Session->read("Username");
		$datas = $this->User->find('all',array('conditions' => array('User.user_name' => $ten)));
		$user = $datas[0]; 
		$this->set("user",$user);
	}
	function quanli_user(){
		$this->layout = 'admin_quanli';
		//$ten = $this->Session->read("Username");
		//$datas = $this->User->find('all',array('conditions' => array('User.user_name' => $ten)));
		//$user = $datas[0]; 
		//$this->set("user",$user);
		$datas = $this->User->find('all');
		$this->set('datas', $datas);
	}
	//--------------Xem thong tin cac tin dang------------------
	function quanli_post() {
		$this->layout = 'admin_quanli';
		$datas = $this->Post->find('all');
		$this->set('datas', $datas);
	}
	//xem thong tin cua cac user
	function adminview_user($id = null) {
		$this->layout = 'admin_quanli';
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect('/');
		}
		$this->set('user', $this->User->read(null, $id));		
	}
	//------------Admin edit user------------------------------
	function adminedit_user($id = null) {
		$this->layout = 'admin_quanli';
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'quanli_user'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
	}
	function danhsachtindang(){
		$this->layout = 'user_hoan';
		$ten = $this->Session->read("Username");
		$datas = $this->User->find('all',array('conditions' => array('User.user_name' => $ten)));
		$user = $datas[0];
		if($user['User']['role'] =="admin"){
			//neu la admin thi duoc quyen xem het cac tin
			$data = $this->Post->find('all');
		}
		else {
			// neu la thanh vien
		$data = $this->Userpost->find('all', array('conditions' =>array('Userpost.user_id' => $user['User']['id'])));
		}
		$this->set("data", $data);
	}
	function dangkithanhvien() {
		$this->layout = 'khung_trangchu';
		if (!empty($this->data)) {
			$this->User->create();
			$this->User->set(array(	'hoten' => $this->data['User']['hoten'],
									'matkhau' => sha1($this->data['User']['matkhau']),
									'user_name' => $this->data['User']['user_name'],
									'email'	=> $this->data['User']['email'],
									'didong' => $this->data['User']['didong'],
									'yahoo' => $this->data['User']['yahoo'],
									'role' =>  $this->data['User']['role'],
				));
			if ($this->User->save()) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('controller' => 'users', 'action' => 'Login'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
	}
	function demuser(){
		$dem = $this->User->find('count');
		return $dem;
	}
	//--------lay thong de kiem tra admin, user-- de sua o header-------------
	function thongtin_user() {
		$ten = $this->Session->read("Username");
		$datas = $this->User->find('all',array('conditions' => array('User.user_name' => $ten)));
		$user = $datas[0];
		return $user;
	}
	//--------------admin quan li phan catalog----------------------
	function quanli_catalog(){
		$this->layout = 'admin_quanli';
		$datas = $this->Catalog->find('all');
		$this->set('datas', $datas);
	}
}
